package com.ruoyi.thgd.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.thgd.domain.*;
import com.ruoyi.thgd.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 退化耕地- 紧实度 -环境参数Controller
 *
 * @author chenhs
 * @date 2024-09-11
 */
@RestController
@RequestMapping("/thgd/jsdHjcs")
public class ThgdJsdHjcsController extends BaseController
{
    @Autowired
    private IThgdJsdHjcsService thgdJsdHjcsService;

    @Autowired
    private IThgdJsdImageService thgdJsdImageService;
    @Autowired
    private IThgdJsdViddesService thgdJsdViddesService;
    @Autowired
    private IThgdJsdChqService thgdJsdChqService;
    @Autowired
    private IThgdPhCgqService thgdPhCgqService;
    @Autowired
    private IThgdYjzCgqService thgdYjzCgqService;
    @Autowired
    private IThgdYjzYsgpService thgdYjzYsgpService;


    /**
     * 查询退化耕地- 紧实度 -环境参数列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ThgdJsdHjcs thgdJsdHjcs)
    {
        //startPage();
        List<ThgdJsdHjcs> list = thgdJsdHjcsService.selectThgdJsdHjcsList(thgdJsdHjcs);
        return getDataTable(list);
    }

    /**
     * 导出退化耕地- 紧实度 -环境参数列表
     */
    @Log(title = "退化耕地- 紧实度 -环境参数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ThgdJsdHjcs thgdJsdHjcs)
    {
        List<ThgdJsdHjcs> list = thgdJsdHjcsService.selectThgdJsdHjcsList(thgdJsdHjcs);
        ExcelUtil<ThgdJsdHjcs> util = new ExcelUtil<ThgdJsdHjcs>(ThgdJsdHjcs.class);
        util.exportExcel(response, list, "退化耕地- 紧实度 -环境参数数据");
    }

    /**
     * 获取退化耕地- 紧实度 -环境参数详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        JSONObject result = new JSONObject();
        ThgdJsdHjcs hjcsInfo =  thgdJsdHjcsService.selectThgdJsdHjcsById(id);
        if(hjcsInfo!=null){
            result.put("hjcsInfo", hjcsInfo);
            ThgdJsdImage imageQuery = new ThgdJsdImage();
            imageQuery.setRid(hjcsInfo.getHjId());
            List<ThgdJsdImage> imageList = thgdJsdImageService.selectThgdJsdImageList(imageQuery);
            result.put("imageList", imageList);

            //视频list
            ThgdJsdViddes viddesQuery = new ThgdJsdViddes();
            viddesQuery.setRid(hjcsInfo.getHjId());
            List<ThgdJsdViddes> videoList = thgdJsdViddesService.selectThgdJsdViddesList(viddesQuery);
            result.put("videoList", videoList);

            //紧实度传感器
            ThgdJsdChq chqQuery = new ThgdJsdChq();
            chqQuery.setRid(hjcsInfo.getHjId());
            List<ThgdJsdChq> chqList = thgdJsdChqService.selectThgdJsdChqList(chqQuery);
            result.put("jsdCgqList", chqList);

            //PH电导率
            ThgdPhCgq phCgqQuery = new ThgdPhCgq();
            phCgqQuery.setRid(hjcsInfo.getHjId());
            List<ThgdPhCgq> phCgqList = thgdPhCgqService.selectThgdPhCgqList(phCgqQuery);
            result.put("phCgqList", phCgqList);

            //有机质
            ThgdYjzCgq yjzCgqQuery = new ThgdYjzCgq();
            yjzCgqQuery.setRid(hjcsInfo.getHjId());
            List<ThgdYjzCgq> yjzCgqList = thgdYjzCgqService.selectThgdYjzCgqList(yjzCgqQuery);
            result.put("yjzCgqList", yjzCgqList);

            //原始光谱
            ThgdYjzYsgp yjsgQuery = new ThgdYjzYsgp();
            yjsgQuery.setRid(hjcsInfo.getHjId());
            List<ThgdYjzYsgp> yjsgList = thgdYjzYsgpService.selectThgdYjzYsgpList(yjsgQuery);
            result.put("yjzYsgpList", yjsgList);


        }

        return success(result);
    }

    /**
     * 获取退化耕地- 紧实度 -环境参数详细信息
     * 获取最新环境参数
     */
    @GetMapping(value = "/hjcsNew")
    public AjaxResult hjcsNew()
    {
        JSONObject result = new JSONObject();
        ThgdJsdHjcs hjcsInfo =  thgdJsdHjcsService.selectThgdJsdHjcsNew();
        if(hjcsInfo!=null){
            result.put("hjcsInfo", hjcsInfo);
            ThgdJsdImage imageQuery = new ThgdJsdImage();
            imageQuery.setRid(hjcsInfo.getHjId());
            List<ThgdJsdImage> imageList = thgdJsdImageService.selectThgdJsdImageList(imageQuery);
            result.put("imageList", imageList);

            //视频list
            ThgdJsdViddes viddesQuery = new ThgdJsdViddes();
            viddesQuery.setRid(hjcsInfo.getHjId());
            List<ThgdJsdViddes> videoList = thgdJsdViddesService.selectThgdJsdViddesList(viddesQuery);
            result.put("videoList", "");

            //紧实度传感器
            ThgdJsdChq chqQuery = new ThgdJsdChq();
            chqQuery.setRid(hjcsInfo.getHjId());
            List<ThgdJsdChq> chqList = thgdJsdChqService.selectThgdJsdChqList(chqQuery);
            result.put("jsdCgqList", chqList);

            //PH电导率
            ThgdPhCgq phCgqQuery = new ThgdPhCgq();
            phCgqQuery.setRid(hjcsInfo.getHjId());
            List<ThgdPhCgq> phCgqList = thgdPhCgqService.selectThgdPhCgqList(phCgqQuery);
            result.put("phCgqList", phCgqList);

            //有机质
            ThgdYjzCgq yjzCgqQuery = new ThgdYjzCgq();
            yjzCgqQuery.setRid(hjcsInfo.getHjId());
            List<ThgdYjzCgq> yjzCgqList = thgdYjzCgqService.selectThgdYjzCgqList(yjzCgqQuery);
            result.put("yjzCgqList", yjzCgqList);

            //原始光谱
            ThgdYjzYsgp yjsgQuery = new ThgdYjzYsgp();
            yjsgQuery.setRid(hjcsInfo.getHjId());
            List<ThgdYjzYsgp> yjsgList = thgdYjzYsgpService.selectThgdYjzYsgpList(yjsgQuery);
            result.put("yjzYsgpList", yjsgList);


        }

        return success(result);
    }

    /**
     * 按日期分组 获取每天最新一条数据
     */
    @GetMapping("/hjcaListGroupByDate")
    public TableDataInfo hjcaListGroupByDate(ThgdJsdHjcs thgdJsdHjcs)
    {
        List<ThgdJsdHjcs> list = thgdJsdHjcsService.selectThgdJsdHjcsListGroupByDate(thgdJsdHjcs);
        return getDataTable(list);
    }

    /**
     * 新增退化耕地- 紧实度 -环境参数
     */
    @Log(title = "退化耕地- 紧实度 -环境参数", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ThgdJsdHjcs thgdJsdHjcs)
    {
        return toAjax(thgdJsdHjcsService.insertThgdJsdHjcs(thgdJsdHjcs));
    }

    /**
     * 修改退化耕地- 紧实度 -环境参数
     */
    @Log(title = "退化耕地- 紧实度 -环境参数", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ThgdJsdHjcs thgdJsdHjcs)
    {
        return toAjax(thgdJsdHjcsService.updateThgdJsdHjcs(thgdJsdHjcs));
    }

    /**
     * 删除退化耕地- 紧实度 -环境参数
     */
    @Log(title = "退化耕地- 紧实度 -环境参数", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(thgdJsdHjcsService.deleteThgdJsdHjcsByIds(ids));
    }
}
